﻿/// <reference path="../core/jquery-1.4.2.min-vsdoc.js"/>
/// <reference path="../config/sw-config-default.js"/>
/// <reference path="../core/sw-prototype.js"/>
/// <reference path="../core/sw.js"/>
/// <reference path="../core/sw-control-core.js"/>

/***************************************************************************
jQuery viewer
NOTE: Created for SmartUI. 
Copyright 2010, Roy zhang
Features：
1.
2.
3.
4.
Update Note：

Needs：
****************************************************************************/
///#region 设置
var viewerSetting = {
    id: '_viewer'
};
///#endregion
(function ($) {
    $.fn.viewer = function (options) {
        /// <summary>pager</summary>
        /// <param name="options" type="Array">配置项</param>
        /// <param name="datas" type="Json">数据源</param>   
        options = $.extend({
            css: 'viewer'
                , csszoomfit: 'zoomfit'
                , bgcolor: '#eee'
                , contentselector: null
                , fixheight: 4
                , fixwidth: 0
        }, options);

        var  bg = options.bgcolor, fixheight = options.fixheight, fixwidth = options.fixwidth, contentselector = options.contentselector;
        return this.each(function () {
            var ctr = $(this),id = ctr.attr('id');
            var tools = $(document.createElement('div')).attr('id', id + viewerSetting.id).addClass(options.css);
            //zoom max fix
            $(document.createElement('a')).addClass(options.csszoomfit).click(function () {
                var btn = $(this),ctr = $('#' + id);
                if (!ctr.data('set'))
                    ctr.data('set', { width: ctr.width(), height: ctr.height(), bg: ctr.css('background-color') });
                btn.toggleClass('max');
                if (btn.hasClass('max')) {
                    var win = $(window);
                    zoomStruct(ctr, 9999, 'absolute', win.width() + win.scrollLeft() - 10, win.height() + win.scrollTop() - 35, contentselector, bg, fixwidth, fixheight);
                    btn.next().hide().next().hide();
                }
                else {
                    var set = ctr.data('set');
                    zoomStruct(ctr, '', 'static', set.width, set.height, contentselector, set.bg, fixwidth, fixheight);
                    btn.next().show().next().show();
                }
            }).appendTo(tools);
            ctr.prepend(tools);
            fixheight += tools.getCss('height');
            var ctrheight = ctr.getCss('height');
            ctr.dblclick(function(){
                $('#' + $(this).attr('id') + viewerSetting.id + ' > a').click();
            });
            if(ctrheight)
            {
                ctr.css('height',ctrheight + fixheight);
                fixheight = ctrheight + fixheight - ctr.find(contentselector).getCss('height');
            }
            //            if (contentselector)
            //                fixwidth = ctr.width() - ctr.find(contentselector).width();
        });
    };

    ///#region 公共设置方法
    function zoomStruct(ctr, zindex, post, width, height, contentselector, bg, fixwidth, fixheight) {
        ctr.css({ "z-index": zindex, position: post, width: width, "height": height, 'background-color': bg, left: 0, top: 0 });
        if (contentselector)
            ctr.find(contentselector).css({ width: ctr.getCss('width') - fixwidth, height:ctr.getCss('height') - fixheight });
    }
    ///#endregion

    ///#region 公共方法
    $.viewerGetHeight = function (ctr) {
       return ctr.children('#' + ctr.attr('id') + viewerSetting.id).height();
    }
    ///#endregion
})(jQuery);